<!--
	Desc: 	车辆表的编辑界面
	Author: caochaofeng
	Date: 	2018-08-19 17:21:27
-->

<template>
  <el-dialog
    :title="!dataForm.id ? '新增车辆' : '修改车辆'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="车号" prop="vecNo">
        <el-input v-model="dataForm.vecNo" placeholder="车号"></el-input>
      </el-form-item>
      <el-form-item label="所属公司" prop="ownedCompany">
        <el-select v-model="dataForm.entpPk" clearable placeholder="请选择运输企业"  @change="selectHandle">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as API  from '@/api/vec'

  export default {
    data() {
      return {
        visible: false,
        options: [{
          value: 150,
          label: '卓承'
        }, {
          value: 152,
          label: '宏发'
        }, {
          value: 154,
          label: '海安销售'
        }, {
          value: 155,
          label: '镇石'
        }, {
          value: 156,
          label: '卓远'
        }],
        dataForm: {
          vecPk: 0,
          vecNo: '',
          entpPk: '',
          ownedCompany: '',
          catCd: '',
          catNmEn: '',
          catNmCn: '',
          chassisNo: '',
          bsCatCd: '',
          businessScope: '',
          apprvWeight: '',
          selfWeight: '',
          trailerNo: ''
        },
        dataRule: {
          vecNo: [
            {required: true, message: '车号不能为空', trigger: 'blur'}
          ],
          ownedCompany: [
            {required: true, message: '所属公司不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.vecPk = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.vecPk) {
             API.info(this.dataForm.vecPk).then((data) => {
              if (data && data.code === 0) {
                this.dataForm = data.vec
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = this.dataForm
            var tick = !this.dataForm.vecPk ?  API.add(params) :  API.update(params)
            tick.then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      selectHandle(value) {
        let obj = this.options.find((item) => {
          return item.value === value;
        });
        if (obj) {
          this.$set(this.dataForm, 'ownedCompany', obj.label);
        } else {
          this.$set(this.dataForm, 'ownedCompany', '');
        }

      },
    }
  }
</script>
